var express = require('express');
var router = express.Router();
const sql = require("../sql/sql");
const banner = require('../sql/banner');
const uuid = require("node-uuid");


router.get('/', function(req, res, next) {
  sql.find(banner,{},{}).then(data=>{
    res.render('banner', { i:5 ,data});
  })
});

router.post('/add', function(req, res, next) {
  let {tit,alt,img} = req.body;
  sql.insert(banner,{
    bannerId:"banner" + uuid.v1(),
    tit,
    alt,
    img
  }).then(()=>{
    res.send("ok");
  })
});

router.get('/delete',function(req,res,next){
  let obj = req.query;
  sql.delete(banner,{bannerId:obj.id},1).then(()=>{
    res.redirect('/banner');
  })
})

module.exports = router;
